<script setup lang="ts">
import {SELL_STATUS} from "~/const/common";
import {EXAMINE_STATUS} from "~/const/Maps";

definePageMeta({
  layout:'admin',
  title:'我的发布列表'
});
const form=reactive({
  limit:20,
  page:1,
  name:'',//模糊搜索-域名
});
const {data:list,refresh}=await useServerGet('/agent/getDomainList',form)
const deleteDomain=(id:string)=>{
  ElMessageBox.confirm('确定下架该域名吗？','提示',{
    confirmButtonText:'确定',
    cancelButtonText:'取消',
    type:'warning'
  }).then(async ()=>{
    const {code}=await usePost('/agent/deleteDomain',{id})
    if(code===200){
      ElMessage.success('删除成功')
      refresh&&refresh();
    }
  })
}
const modifyPrice=(row:any)=>{
  ElMessageBox.prompt('请输入新的价格','提示',{
    confirmButtonText:'确定',
    cancelButtonText:'取消',
    //1.31
    inputPattern:/^\d+(\.\d{1,2})?$/,
    inputErrorMessage:'请输入数字',
    inputValue:row.price,
    inputType:'number',
    center:true,
  }).then(async ({value})=>{
    const {code,data}=await usePost('/agent/updateGoods',{id:row.id,price:value})
    if(code===200){
      ElMessage.success('修改成功')
      refresh&&refresh();
    }
  })
}
const cancel=(row)=>{
  ElMessageBox.confirm('确定取消该域名代卖吗？','提示',{
    confirmButtonText:'确定',
    cancelButtonText:'取消',
    type:'warning'
  }).then(async ()=>{
    const {code}=await usePost('/agent/cancel',{id:row.id})
    if(code===200){
      ElMessage.success('取消成功')
      refresh&&refresh();
    }
  })
}
const search=()=>{
  refresh&&refresh();
}
</script>

<template>
<div id="container">
  <el-affix :offset="124" target="#container">
    <el-card>
      <div class="flex">
        <div class="mr-20">
          <el-button type="danger"><NuxtLink to="/admin/agent/publish">发布域名</NuxtLink></el-button>
        </div>
        <div class="w-52 mr-2">
          <el-input placeholder="请输入域名" v-model="form.name" class="w-40" clearable></el-input>
        </div>
        <div class="w-20">
          <el-button type="primary" @click="search">搜索</el-button>
        </div>
      </div>
    </el-card>
  </el-affix>
  <el-card class="mt-4">
    <el-table :data="list.data.list" :header-cell-style="{'text-align':'center'}" :cell-style="{'text-align':'center'}" class="h-[620px] overflow-auto">
      <el-table-column prop="id" label="ID"></el-table-column>
      <el-table-column prop="domain" label="域名" min-width="150"></el-table-column>
      <el-table-column label="长度">
        <template #default="{row}">
          <span>{{row.domain.endsWith('.com.cn')?row.domain.split('.').slice(0,-2).join('.').length:row.domain.split('.').slice(0,-1).join('.').length}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="price" label="价格"></el-table-column>
<!--      <el-table-column prop="user_phone" label="手机号"></el-table-column>-->
<!--      <el-table-column prop="synopsis" label="域名简介"></el-table-column>-->
      <el-table-column prop="expiration_time" label="到期时间"></el-table-column>
      <el-table-column prop="state" label="状态">
        <template #default="{row}">
          {{SELL_STATUS[row.state]}}
        </template>
      </el-table-column>
      <el-table-column prop="state" label="审核状态">
        <template #default="{row}">
          {{EXAMINE_STATUS[row.examine_status]}}
        </template>
      </el-table-column>
      <el-table-column label="操作列表">
        <template #default="scope">
          <div class="flex justify-center">
            <el-dropdown>
              <el-button>
                更多操作<i class="el-icon-arrow-down el-icon--right"></i>
              </el-button>
              <template #dropdown>
                <el-dropdown-menu >
                  <el-dropdown-item @click="cancel(scope.row)">取消</el-dropdown-item>
                  <!--                  push-->
                  <el-dropdown-item @click="modifyPrice(scope.row)">修改价格</el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
            <el-button type="danger" class="ml-1" @click="deleteDomain(scope.row.id)" v-if="scope.row.state==1">下架</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <MyPage v-model:page="form.page" v-model:perpage="form.limit" v-model:total="list.data.total" @change="refresh"></MyPage>
  </el-card>
</div>
</template>

<style scoped lang="scss">

</style>